﻿/**
**********************************************************************************
*   Facebook.MultiShare
*   Standard FB Share allowing customized layouts/callbacks
**********************************************************************************
*/
Facebook.MultiShare = function(config) {
	this.showLink = config.showLink;
	this.hideLink = config.hideLink;
	this.fbSettings = config.fbSettings;
	
    this.init(); // constructor call
};
Facebook.MultiShare.prototype = {
    // constructor
    init: function()
    {
        // DOM element cache
        this.overlay = $('<div id="fbms-overlay" />');
		this.shareModal = $('#fbms-modal-o');
		this.friends = $('ul#friends');
		this.notifications = $('#fbms-notifications');
		this.loading = $('<img alt="loading..." src="/areas/facebookapp/images/loading.gif" width="220" height="19" />');
        this.postSuccessMessage = 'Your message has been sent.';
        this.postBlockedMessage = 'The person you are attempting to send to has blocked messages from this application, select another friend to continue.';
        this.postLimitMessage = 'You have reached the Facebook daily sharing limit. Come back tomorrow to share again!';
        this.postErrorMessage = 'We\'re sorry, there was an error sending your message. Please go back and try again!';

        // setup methods
		this.attachEvents();
		this.addOverlay();
        this.buildFriendsList();
    },

    attachEvents: function()
    {
        // global listeners
		this.showLink.live('click', $.proxy(function() { this.shareModal.show(); this.overlay.show(); }, this));
		this.hideLink.live('click', $.proxy(function() { this.shareModal.hide(); this.overlay.hide(); }, this));
	},

    addOverlay: function()
    {
        $('body').prepend(this.overlay);
	},

    buildFriendsList: function()
    {
		// sort friends by name (facebook style)
		sortByName = function (a, b) {
			var x = a.name.toLowerCase();
			var y = b.name.toLowerCase();
			return ((x < y) ? -1 : ((x > y) ? 1 : 0));
		};

        // get friend data and build friends list using tmpl plugin
        FB.api('/me/friends/', { fields: ['id', 'name', 'first_name', 'last_name'] }, $.proxy(function (response) {
            response.data.sort(sortByName);

            $('#tmpl-friends').tmpl(response.data).appendTo(this.friends);
			this.configureFriendSelector();
        }, this));
    },

    configureFriendSelector: function()
    {
        $.fbMultiShare({
            submitButtonClick: this.onSubmitButtonClick.bind(this)
            , submitButtonSuccess: this.onWallPostSuccess.bind(this)
            , submitButtonError: this.onWallPostError.bind(this)
            , cancelButtonClick: this.onCancelButtonClick.bind(this)
			, wpLinkURL: this.fbSettings.link
            , wpLinkText: this.fbSettings.name
            , wpPicture: this.fbSettings.picture
            , wpCaption: this.fbSettings.caption
            , wpDescription: this.fbSettings.description
        });
	},

    onSubmitButtonClick: function(options)
    {
		this.notifications.show();
		this.notifications.html(this.loading);
    },

    onWallPostSuccess: function(options, response)
    {
		this.notifications.html(this.postSuccessMessage);
    },

    onWallPostError: function(options, response)
    {
		// blocked by user
		if (response.error.message.indexOf('210') !== -1) {
    		this.notifications.html(this.postBlockedMessage);

		// limit reached
		} else if (response.error.message.indexOf('341') !== -1) {
    		this.notifications.html(this.postLimitMessage);

		// generic error
		} else {
    		this.notifications.html(this.postErrorMessage);
		}
    },

    onCancelButtonClick: function(options)
    {
		this.notifications.hide();
    }
};
